package com.rank.test;

import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;

public class UserInfo{
	private String userId;
	private Jedis redisService;

	public UserInfo() {
		this.redisService = new Jedis("192.168.1.143",6379);
	}

	/**
	 * @description 注册或更新用户信息方法实现
	 * @date 2013-1-22
	 * @parameter
	 * @see com.optest.test.UserInfo#add(java.lang.String)
	 */
	public void add(String key,int score,String userId) {
		this.redisService.zadd(key,score,userId); // add
	}
	
	/**
	 * @description 注册或更新用户信息方法实现
	 * @date 2013-1-22
	 * @parameter
	 * @see com.optest.test.UserInfo#add(java.lang.String)
	 */
	public void adds(String key,Map<Double, String> scoreMembers) {
		this.redisService.zadd(key,scoreMembers); // add
	}	
	
	/**
	 * @description 获取用户分数排名方法实现
	 * @date 2013-1-22
	 * @parameter
	 * @see com.optest.test.UserInfo#getIndex(java.lang.String)
	 */
	public void getIndex(String key,String userId) {
		System.out.println(this.redisService.zrevrank(key, userId)); //getIndex
	}
	
	/**
	 * @description 获取用户分数排名方法实现
	 * @date 2013-1-22
	 * @parameter
	 * @see com.optest.test.UserInfo#getScore(java.lang.String)
	 */
	public void getScore(String key,String userId1) {
		System.out.println(this.redisService.zscore(key, userId1)); //getScore
	}

	/**
	 * @description 获取当前用户所有关注对象的用户编号集合
	 * @date 2013-1-22
	 * @parameter
	 * @see com.optest.test.UserInfo#following()
	 */
	public Set<String> smembers() {
		return this.redisService.smembers("graph:user:" + this.userId
				+ ":following"); // member
	}

	/**
	 * @description 获取当前用户被哪些人关注的用户编号集合
	 * @date 2013-1-22
	 ** @parameter
	 * @see com.optest.test.UserInfo#followedBy()
	 */
	public Set<String> followedBy() {
		return this.redisService.smembers("graph:user:" + this.userId
				+ ":followed_by"); // member
	}

	/**
	 * @description 取消关注某人传入的用户编号标识操作方法实现
	 * @date 2013-1-22
	 * @parameter
	 * @see com.optest.test.UserInfo#unfollow(java.lang.String)
	 */
	public void unfollow(String userId) {
		this.redisService.srem("graph:user:" + this.userId + ":following",
				userId); // remove
		this.redisService.srem("graph:user:" + userId + ":followed_by",
				this.userId); // remove
	}

	/**
	 * @description 判断当前用户是否关注了对应用户编号的用户方法实现
	 * @date 2013-1-22
	 * @parameter
	 * @see com.optest.test.UserInfo#isfollowing(java.lang.String)
	 */
	public boolean isfollowing(String userId) {
		return this.redisService.sismember("graph:user:" + this.userId
				+ ":following", userId); // is member
	}

	/**
	 * @description 判断是否存在对应用户编号的粉丝方法实现
	 * @date 2013-1-22
	 * @parameter
	 * @see com.optest.test.UserInfo#isfollowedBy(java.lang.String)
	 */
	public boolean isfollowedBy(String userId) {
		return this.redisService.sismember("graph:user:" + this.userId
				+ ":followed_by", userId);// is member
	}

	/**
	 * @description 统计当前用户关注的人数总和
	 * @date 2013-1-22
	 * @parameter
	 * @see com.optest.test.UserInfo#followingCount()
	 */
	public Long followingCount() {
		return this.redisService.scard("graph:user:" + this.userId
				+ ":following"); // card
	}

	/**
	 * @description 统计当前用户有多少粉丝数目方法实现
	 * @date 2013-1-22
	 * @parameter
	 * @see com.optest.test.UserInfo#followerCount()
	 */
	public Long followerCount() {
		return this.redisService.scard("graph:user:" + this.userId
				+ ":followed_by"); // card
	}

	/**
	 * @description 获取当前用户和传入用户编号对应的用户共同关注的用户编号集合方法实现
	 * @date 2013-1-22
	 * @parameter
	 * @see com.optest.test.UserInfo#commonfollowing(java.lang.String)
	 */
	public Set<String> commonfollowing(String userId) {
		String s1 = "graph:user:" + this.userId + ":following";
		String s2 = "graph:user:" + userId + ":following";
		return this.redisService.sinter(new String[] { s1, s2 });
	}

	/**
	 * @description 获取当前用户和传入用户编号对应的用户共同粉丝的用户编号集合方法实现
	 * @date 2013-1-22
	 * @parameter
	 * @see com.optest.test.UserInfo#commonfollowedBy(java.lang.String)
	 */
	public Set<String> commonfollowedBy(String userId) {
		String s1 = "graph:user:" + this.userId + ":followed_by";
		String s2 = "graph:user:" + userId + ":followed_by";
		return this.redisService.sinter(new String[] { s1, s2 });
	}
}
